package com.cyt.web.controller.work.wb;

import cn.hutool.core.util.RandomUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.util.SMSDto;
import com.cyt.sys.util.SMSUtils;
import com.cyt.web.controller.base.BaseController;
import com.cyt.work.wb.model.vo.WbCheckCodeVo;
import com.cyt.work.wb.service.WbCheckCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 网站模块-验证码controller层
 */

@RestController
@Slf4j
public class WbCheckCodeController extends BaseController {

    @Resource
    private WbCheckCodeService wbCheckCodeService;

    @RequestMapping(value = "/wbCheckCode/page",method = RequestMethod.POST)
    public ReturnResult page(WbCheckCodeVo webCheckCodeVo, int pageSize, int pageNum){
        return wbCheckCodeService.page(webCheckCodeVo,pageSize,pageNum);
    }

    /**
     * 发送短信验证码
     */
    @RequestMapping(value = "/smsSend")
    public ReturnResult smsSend(String phone) {
        // 怎么限定发送短信，1分钟内限定发送次数，数据库限定，这种理论上属于黑名单
        Integer info = wbCheckCodeService.selectMinuteCountByPhone(phone);
        if (info > 0) {
            log.info("手机号是：{}，短信1分钟内不可重复发送", phone);
            return new ReturnResult(ErrorCode.CODE_1, "短信1分钟内不可重复发送");
        }
        String code = RandomUtil.randomNumbers(6);
        // JuHeResultVo juHeResultVo = SmsApi.smsSend(phone, SmsApi.tpl_id, "#code#=" + code + "&#m#=15");
        String param = "{\"code\":\"" + code + "\"}";
        /*ReturnEntity returnEntity = AliSmsApi.send(phone, AliSmsApi.TEMPLATE_CODE, param);
        if (returnEntity.getCode() == 0) {
            log.info("手机号是：{}，短信发送成功，短信码为：{}", phone, code);
            return wbCheckCodeService.addOrUpdate(new WbCheckCodeVo(phone, code, ysq.xyg.base.util.DateUtil.getMinuteOneDate(new Date(), 15),
                    BaseConstant.BASE_USE_STATUS_WSY), null);
        } else {
            log.info("手机号是：{}，短信发送失败，错误原因为：{}", phone, returnEntity.getMsg());
            return returnEntity;
        }*/
        Map<String,String> map= new HashMap<String,String>();
        map.put("code",code);
        SMSUtils.sendVerifyCodeByApply(new SMSDto(DictCode.SYS_SMS_PLATFORM,phone,"1",map));
        return new ReturnResult();
    }
}